<?php

class Reale_Model_Estate_ProjectMapper 
{
    
	/**
     * @var Zend_Db_Table_Abstract
     */
    protected $_dbTable;
    

    /**
     * Specify Zend_Db_Table instance to use for data operations
     * 
     * @param  Zend_Db_Table_Abstract $dbTable 
     * @return Reale_Model_Estate_ProjectMapper
     */
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    /**
     * Get registered Zend_Db_Table instance
     *
     * Lazy loads Reale_Model_DbTable_Property if no instance registered
     * 
     * @return Zend_Db_Table_Abstract
     */
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Reale_Model_DbTable_Project');
        }
        return $this->_dbTable;
    }
    
    
    /**
     * 
     * To save new or update existing agent's informtion.
     * @param Reale_Model_Estate_Project
     * @return Reale_Model_Estate_Project | null 
     */
    public function save(Reale_Model_Estate_Project &$project){
    	$commonData = array(
    			'category_id'=>$project->getCategory_id(),
    			'type_id'=>$project->getType_id(),
                'city'=>$project->getCity(),
                'count'=>$project->getCount(),
                'location'=>$project->getLocation(),
                'bhk'=>$project->getBhk(),
                'rate'=>$project->getRate(),
                'size'=>$project->getSize(),
    			'size_units'=>$project->getSize_units(),
                'state'=>$project->getState(),
                'summary'=>$project->getSummary());

        $id = $project->getEstate_id();
        if ($id) {
            $rowsUpdated = $this->getDbTable()->update(
            array('estate_id = ?' => $id));
            return $rowsUpdated;
        } else {
            $estateTable = new Reale_Model_DbTable_Estate();
            try {
                $this->getDbTable()
                    ->getAdapter()
                    ->beginTransaction();
                $pKey = $estateTable->insert($commonData);
                $project->setEstate_id($pKey['estate_id']);
                
                $projData = array('project_id' => $project->getEstate_id(), 
                'project_name' => $project->getProject_name(), 
                'builder' => $project->getBuilder(), 
                'area' => $project->getArea());
                
                $this->getDbTable()->insert($projData);
                
                $this->getDbTable()
                    ->getAdapter()
                    ->commit();
            } catch (Zend_Exception $e) {
                $this->getDbTable()
                    ->getAdapter()
                    ->rollBack();
                $logger = new Zend_Registry('logger');
                $logger->debug($e);
                throw $e;
            }
            
            return $project;
        }
        
        return null;
    }
	
}

